package com.example.mvvm.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private MyThread myThread;
    private Paint paint;
    private  boolean isDraw = true;
    private int centerX, centerY, radiu;

    private int CHANGEX, CHANGEY;

    public MySurfaceView(Context context) {
        this(context, null);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    private void init() {
        //TODO surface的控制器
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        myThread = new MyThread(holder);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        centerX = 80;
        centerY = 80;
        radiu = 80;
        CHANGEX = 5;
        CHANGEY = 5;
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {
        myThread.start();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        isDraw = false;
    }

    public  class MyThread extends Thread{
        private  SurfaceHolder holder;

        public  MyThread(SurfaceHolder holder){
            this.holder= holder;
        }

        @Override
        public void run() {
            //TODO 绘制Surface界面
            Canvas canvas = null;
            while (isDraw) {
                //加入同步防止多个子线程同时操作surfaceview
                synchronized (holder) {
                    //加入try-catch程序正常运行
                    try {
                        //锁定画布并获取画布对象
                        canvas = holder.lockCanvas();
                        doDraw(canvas);
                        sleep(16);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (canvas != null) {
                            //解锁画布
                            holder.unlockCanvasAndPost(canvas);
                        }
                    }
                }
            }
        }
    }

    private void doDraw(Canvas canvas) {
        //TODO 画圆
        canvas.drawColor(Color.WHITE);//给画布设置背影颜色
        canvas.drawCircle(centerX, centerY, radiu, paint);

        //todo 当x轴小于球的半径或者x轴加上球的半径大于画布的宽度，那么就移动球的位置。
        if(centerX-radiu<0|| centerX+radiu>getWidth()){
            CHANGEX = -CHANGEX;
        }
        if(centerY-radiu<0||centerY+radiu>getHeight()){
            CHANGEY = -CHANGEY;
        }
        //TODO 实现动画效果
        centerX += CHANGEX;
        centerY += CHANGEY;
    }
}
